package service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author: jie.zhao
 * @create: 2021/03/20 21:36
 */
public class StaticResourceUtil {

    /**
     * 获取绝对路径
     * @param path
     * @return
     */
    public static String getAbsolutePath(String path){
        String absolutePath = StaticResourceUtil.class.getResource("/").getPath();
        return absolutePath.replaceAll("\\\\","/")+path;
    }

    public static void outPutResource(InputStream inputStream, OutputStream outputStream) throws IOException {
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        int resources = count;
        outputStream.write(HttpProtocolUtil.getHeader200(resources).getBytes());

        long writeen = 0;

        int byteSize = 1024;

        byte[] bytes = new byte[byteSize];

        while (writeen < count) {
            if (writeen + byteSize > resources) {
                byteSize = (int) (resources - writeen);
                bytes = new byte[byteSize];
            }
            inputStream.read(bytes);
            outputStream.write(bytes);

            outputStream.flush();
            writeen+=byteSize;
        }

    }
}
